package com.projecy.system.controller;

import com.projecy.common.exception.BadRequestException;
import com.projecy.common.utils.ResultUtil;
import com.projecy.logs.annotation.Log;
import com.projecy.system.model.SysRole;
import com.projecy.system.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequiredArgsConstructor
@RequestMapping("/sys")
public class SysRoleController extends ResultUtil {
    @Autowired
    SysRoleService roleService;

    @Log("获取角色列表")
    @GetMapping("/role/table")
    public ResponseEntity<Object> getRoleList(String blurry){
        System.out.println("获取角色列表");
        try {
            return success(true, roleService.getRoleList(blurry));
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

    @Log("编辑角色")
    @PostMapping("/role/edit")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResponseEntity<Object> editRole(@RequestBody SysRole role){
        System.out.println("编辑角色");
        String tag = role.getId() == null ? "添加成功" : "修改成功";
        try {
            roleService.editRole(role);
            return success(true, tag);
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

    @Log("删除角色")
    @DeleteMapping("/role/del")
    public ResponseEntity<Object> delRole(Long id){
        System.out.println("删除角色");
        try {
            roleService.delRole(id);
            return success(true, "删除成功");
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

    @Log("获取角色选择框列表")
    @GetMapping("/role/select")
    public ResponseEntity<Object> getAllRoleForXm(Long userId){
        System.out.println("获取角色选择框列表");
        try {
            return success(true, roleService.getAllRoleForXm(userId));
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }









}
